<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            width: 100%;
            height: 100%;
        }

        #app {
            margin: auto;
            width: 500px;
            height: 100%;
        }

        .inbox {
            width: 100%;
            height: 40px;
            margin-bottom: 10px;
        }

        .inEle {
            display: inline-block;
            width: 420px;
            height: 30px;
            padding-left: 10px;
            box-sizing: border-box;
            margin-top: 10px;
            outline: none;
            /* border: none; */
            border-radius: 5px;
            background-color: palevioletred;
            border: 1px solid plum;

        }

        button {
            display: inline-block;
            width: 50px;
            height: 30px;
            background-color: paleturquoise;
            border: none;
            border-radius: 5px;
            margin-left: 10px;
        }

        .do {
            height: 30px;
            width: 100%;
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;

        }

        .do div {
            width: 25%;
            height: 30px;
            border: 1px solid papayawhip;
            border-radius: 5px;
            text-align: center;
            line-height: 30px;
            font-size: 18px;
        }

        .total {
            background-color: palegoldenrod;
            color: lightblue;
        }

        .fin {
            background-color: palegreen;
            color: lightcoral;
        }

        .fal {
            background-color: red;
            color: aliceblue;
        }

        .title {
            font-size: 20px;
            font-weight: bolder;
        }

        .empty {
            text-align: center;
            color: red;
        }

        .item {
            width: 100%;
            height: 40px;
            line-height: 40px;
            background-color: palevioletred;
            margin-top: 10px;
            display: flex;
        }

        .active {
            background-color: red;
        }

        p {
            box-sizing: border-box;
            display: inline-block;
            padding-left: 20px;
            width: 300px;
        }

        .shan {
            display: inline-block;
            flex: 1;
            padding-left: 100px;
        }

        .name {
            padding-left: 50px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="inbox">
            <input type="text" placeholder="输入任务名称" class="inEle" v-model="inputText"><button @click="add()">新增</button>
        </div>
        <div class="do">
            <div class="total">全部任务({{tasklist.length}})</div>
            <div class="fin">已完成（{{finish}}）</div>
            <div class="fal">未完成（{{unfinish}}）</div>
        </div>
        <div class="list">
            <div class="title">任务列表</div>
            <div :class="{item:true,active:item.status}" v-for="(item,index) in tasklist" :key="index">
                <input type="checkbox" class="change" @click="change(item)" v-bind:checked="item.status">
                <p class="con"><span>{{item.id}}</span><span class="name">{{item.name}}</span></p>
                <span class="shan" @click="del(item)">删除</span>
            </div>
            <div class="empty" v-show="tasklist.length==0">任务列表为空，请添加一个任务!</div>
        </div>
    </div>
</body>

</html>
<script src="./vue-2.5.21.js"></script>
<script>
    var v = new Vue({
        el: '#app',
        data: {
            inputText: '',//保存输入框中的值
            tasklist: [
                /* {id:1, name:'任务名称', status:false } */
            ],
            finish: 0,
            unfinish: 0
        },
        methods: {
            add() {
                if (this.inputText) {
                    var obj = { id: this.tasklist.length + 1, name: this.inputText, status: false }
                    this.tasklist.push(obj)
                    this.inputText = ''
                }
            },
            change(item) {
                item.status = !item.status
            },
            del(item) {
                console.log(item);
                // console.log(item.id);
                this.tasklist.splice(item.id - 1, 1)
            },

        },
        watch: {
            tasklist: {
                handler() {
                    this.finish = this.tasklist.filter((item) => { return item.status }).length;
                    this.unfinish = this.tasklist.length - this.tasklist.filter((item) => { return item.status }).length;
                },
                deep: true,
            }
        }
    })
</script>